package com.example.taximeter.controller;


import com.example.taximeter.bean.FileInfo;
import com.example.taximeter.bean.ParamFile;
import com.example.taximeter.bean.ParamFileView;
import com.example.taximeter.config.GlobalParam;
import com.example.taximeter.service.FileInfoService;
import com.example.taximeter.service.ParamFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 定时任务
 */
@Component
@RestController
public class Schedul {

    private FileInfoService fileInfoService;
    private ParamFileService paramFileService;

    @Autowired
    public Schedul(FileInfoService fileInfoService, ParamFileService paramFileService){
        this.fileInfoService = fileInfoService;
        this.paramFileService = paramFileService;
    }


    //定期清除文件
//    @Scheduled(cron="0 0 21 * * ?")//

    //查看版本升级记录列表
//    @RequestMapping(value = "/ceshi",method = RequestMethod.GET)
    @Scheduled(cron="0 0 3 ? * 7")//每周日三点
    public void clearFile(){
        String classPath = this.getClass().getClassLoader().getResource("").getPath();
//        String path = classPath.substring(0,classPath.indexOf(GlobalParam.filesposition))+"files/";
        String path = classPath+"static/";
        path=path.replaceAll("%20"," ");
        List<FileInfo> fileInfoList = fileInfoService.findFileInfos();
        List<ParamFileView> paramFiles = paramFileService.findParamfileListFile();
        for(FileInfo fileInfo : fileInfoList){
            Boolean ishere = false;
            for(ParamFileView paramFileView : paramFiles){
                if( fileInfo.getFileid().equals(paramFileView.getFileid()) ){
                    ishere = true;
                    break;
                }
            }
            if(!ishere){

                GlobalParam.deleteF(fileInfo, path);
                fileInfoService.deleteFile(fileInfo.getFileid());
            }
        }
//        logger.info("日期"+GlobalParam.getSRq()+" 3点——清除未提交文件！");
    }
}
